/**
 * 
 */
package org.agiba.jira.plugin.action;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.agiba.jira.plugin.model.XWikiCredentials;
import org.agiba.jira.plugin.model.XWikiInstance;
import org.agiba.jira.plugin.model.XWikiSpaceInfo;
import org.agiba.jira.plugin.xwiki.XWikiRPCService;
import org.apache.commons.lang.StringUtils;

import com.atlassian.jira.web.action.JiraWebActionSupport;

/**
 * @author Thorsten Kamann <thorsten.kamann@googlemail.com>
 *
 */
public class XWikiAdminAction extends JiraWebActionSupport {
	private static final long serialVersionUID = 1L;
	private XWikiRPCService service;
	private boolean configure = false;
	private boolean remove = false;
	private String instanceKey = "";
	private String alias = "";
	private String url = "";
	private String[] spaces = new String[0];
	private String active = "";
	private String uid = "";
	private String pwd = "";
	private String cancel = "";
	private String submit = "";
	private String message = "";

	public static String CONFIGURE = "configure";
	public static String CHOOSE_SPACES = "choose-spaces";

	public XWikiAdminAction() {
		service = new XWikiRPCService();
	}

	public List<XWikiInstance> getAllConfiguredXWikiInstances() {
		return service.findAllXwikiInstances();
	}

	public XWikiInstance getXWikiInstance() {
		XWikiInstance instance = null;
		
		if (StringUtils.isNotEmpty(instanceKey)){
			instance = service.findXWikiInstanceByKey(instanceKey);
		}else{
			instance = XWikiInstance.EMPTY;
		}
		return instance;
	}
	
	public List<XWikiSpaceInfo> getSpacesForXWikiInstance(XWikiInstance instance) {
		return service.findAllSpaces(instance);
	}

	public String getSpacesForXWikiInstance(){
		List<XWikiSpaceInfo> spaces;
		XWikiInstance instance;
		
		if (StringUtils.isNotEmpty(instanceKey)) {
			instance = service.findXWikiInstanceByKey(instanceKey);
		} else {
			instance = new XWikiInstance().withUrl(url).withCredentials(
					new XWikiCredentials().withUid(uid).withPwd(pwd));
		}
		
		spaces = getSpacesForXWikiInstance(instance);
		
		
		String result = "";
		for (XWikiSpaceInfo space : spaces) {
			result += space.getKey() + ",";
		}
		if (result.endsWith(",")){
			result = result.substring(0, result.length()-1);
		}
		
		return result;
	}

	public String doDefault() {
		log.debug("Entering doDefault");
		return SUCCESS;
	}
	
	public String doChooseSpaces(){

		System.out.println(url);

		return CHOOSE_SPACES;
	}

	public String doConfigure() {
		log.debug("Entering doConfigure");
		configure = true;
		return CONFIGURE;
	}

	public String doSave() {
		log.debug("Entering doSave");

		List<XWikiSpaceInfo> spaceInfos = new ArrayList<XWikiSpaceInfo>();
		for (int i = 0; i < spaces.length; i++) {
			spaceInfos.add(new XWikiSpaceInfo().withKey(spaces[i]));
		}

		XWikiInstance instance = new XWikiInstance()
				.withAlias(alias)
				.withUrl(url)
				.withSpaces(spaceInfos)
				.withCredentials(
						new XWikiCredentials().withPwd(pwd).withUid(uid));
		
		if (StringUtils.isNotEmpty(instanceKey)) {
			instance.setKey(instanceKey);
		} else {
			instance.setKey(UUID.randomUUID().toString());
		}
		
		
		
		service.saveXWikiInstance(instance);
		return SUCCESS;
	}
	
	public String doRemove(){
		log.debug("Entering doRemove");
		remove = true;

		if (StringUtils.isNotEmpty(cancel)){
			remove = false;
			instanceKey = "";
			return SUCCESS;
		}
		
		if (StringUtils.isNotEmpty(submit)){
			XWikiInstance instance = service.findXWikiInstanceByKey(instanceKey);
			if (instance == null){
				message = "xwiki.admin.error.1";
			}else{
				service.removeXWikiInstance(instance);
				remove = false;
				instanceKey = "";
			}
			
			return SUCCESS;
		}
		
//				
		return SUCCESS;
	}
	

	/**
	 * @return the configure
	 */
	public boolean isConfigure() {
		return configure;
	}

	/**
	 * @return the instanceKey
	 */
	public String getInstanceKey() {
		return instanceKey;
	}

	/**
	 * @param instanceKey
	 *            the instanceKey to set
	 */
	public void setInstanceKey(String instanceKey) {
		this.instanceKey = instanceKey;
	}

	/**
	 * @return the alias
	 */
	public String getAlias() {
		return alias;
	}

	/**
	 * @param alias
	 *            the alias to set
	 */
	public void setAlias(String alias) {
		this.alias = alias;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the spaces
	 */
	public String[] getSpaces() {
		return spaces;
	}

	/**
	 * @param spaces
	 *            the spaces to set
	 */
	public void setSpaces(String[] spaces) {
		this.spaces = spaces;
	}

	/**
	 * @return the active
	 */
	public String getActive() {
		return active;
	}

	/**
	 * @param active
	 *            the active to set
	 */
	public void setActive(String active) {
		this.active = active;
	}

	/**
	 * @return the remove
	 */
	public boolean isRemove() {
		return remove;
	}

	/**
	 * @param cancel the cancel to set
	 */
	public void setCancel(String cancel) {
		this.cancel = cancel;
	}

	/**
	 * @param removeIt the removeIt to set
	 */
	public void setSubmit(String submit) {
		this.submit = submit;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param uid
	 *            the uid to set
	 */
	public void setUid(String uid) {
		this.uid = uid;
	}

	/**
	 * @param pwd
	 *            the pwd to set
	 */
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
}
